package com.chang.rentmall.servicesImpl;

import com.chang.rentmall.beans.MessageBean;
import com.chang.rentmall.beans.ShopBean;
import com.chang.rentmall.daos.ShopDao;
import com.chang.rentmall.forms.GetShopsFormBean;
import com.chang.rentmall.services.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("ShopService")
public class ShopServiceImpl implements ShopService {
    @Autowired
    ShopDao shopDao;

    @Override
    public MessageBean addShop(ShopBean shopBean) {
        MessageBean messageBean = new MessageBean();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
        shopBean.setCreateTime(df.format(new Date()));
        int flag = shopDao.addShop(shopBean);
        if (flag < 1 ) {
            messageBean.setValues(0, "创建失败", "");
        }
        return messageBean;
    }

    @Override
    public MessageBean updateShop(ShopBean shopBean) {
        MessageBean messageBean = new MessageBean();
        int flag = shopDao.updateShop(shopBean);
        if (flag < 1 ) {
            messageBean.setValues(0, "修改失败", "");
        }
        return messageBean;
    }

    @Override
    public MessageBean getShops(GetShopsFormBean getShopsFormBean) {
        MessageBean messageBean = new MessageBean();
        List<ShopBean> list = shopDao.getShops(getShopsFormBean.getQuery(), getShopsFormBean.getPagination());
        messageBean.setBody(list);
        return messageBean;
    }

    @Override
    public MessageBean getShopById(int id) {
        MessageBean messageBean = new MessageBean();
        ShopBean shopBean = shopDao.getShopById(id);
        messageBean.setBody(shopBean);
        return messageBean;
    }

    @Override
    public MessageBean getShopByUserId(int userId) {
        MessageBean messageBean = new MessageBean();
        ShopBean shopBean = shopDao.getShopByUserId(userId);
        messageBean.setValues(200, "查询成功", shopBean);
        return messageBean;
    }
}
